package com.csq.portal.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.csq.cms.pojo.SignInfo;
import com.csq.cms.service.RedisService;
import com.csq.cms.service.SignupService;
import com.csq.common.pojo.ReturnResult;
import com.csq.common.pojo.StatusCode;
import com.csq.common.util.LoginUtil;
import com.csq.manager.pojo.Account;
import com.csq.manager.pojo.Enterprise;
import com.csq.manager.pojo.Signup;
import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("/signup")
public class SignupController {

	@Autowired
	private SignupService signupService;

	@Autowired
	private RedisService redisService;

	@RequestMapping(value = "/add/parttimeId")
	public ReturnResult addSignup(@PathVariable Integer parttimeId,
			HttpServletRequest request) {
		ReturnResult checkAccountLogin = LoginUtil.checkAccountLogin(request);
		if (checkAccountLogin.getState() != StatusCode.SUCCESS) {
			return checkAccountLogin;
		}

		// 取出用户名
		Account account = (Account) request.getSession()
				.getAttribute("account");

		// 更新redis
		Account redisAccount = redisService.getRedisAccount(account.getEmail());

		request.getSession().setAttribute("account", redisAccount);

		Signup signup = new Signup();
		signup.setParttimeId(parttimeId);
		signup.setUserId(redisAccount.getUserId());

		return signupService.addSignup(signup);
	}

	@RequestMapping(value = "/account/getSignInfoByType/{type}")
	public String getSignInfoByTypeAndAccountId(@PathVariable Integer type,
			Integer page, Integer row, HttpServletRequest request) {

		ReturnResult checkAccountLogin = LoginUtil.checkAccountLogin(request);
		if (checkAccountLogin.getState() != StatusCode.SUCCESS) {
			return checkAccountLogin.getMessage();
		}

		Account account = (Account) request.getSession()
				.getAttribute("account");

		PageInfo<SignInfo> signInfo = signupService
				.getSignInfoByAccountIdAndType(account.getUserId(), type, page,
						row);

		request.setAttribute("signInfo", signInfo);

		return "/web/account/signList";

	}

	@RequestMapping(value = "/company/getSignInfoByType/{type}")
	public String getSignInfoByTypeAndEnterpriseId(@PathVariable Integer type,
			Integer page, Integer row, HttpServletRequest request) {

		ReturnResult checkEnterpriseLogin = LoginUtil
				.checkEnterpriseLogin(request);
		if (checkEnterpriseLogin.getState() != StatusCode.SUCCESS) {
			return checkEnterpriseLogin.getMessage();
		}

		Enterprise enterprise = (Enterprise) request.getSession().getAttribute(
				"enterprise");

		PageInfo<SignInfo> signInfo = signupService
				.getSignInfoByEnterpriseIdAndType(enterprise.getEnterpriseId(),
						type, page, row);

		request.setAttribute("signInfo", signInfo);

		return "/web/company/signList";

	}

	public ReturnResult deleteSignup(Integer signupId,
			HttpServletRequest request) {

		ReturnResult checkAccountLogin = LoginUtil.checkAccountLogin(request);
		if (checkAccountLogin.getState() != StatusCode.SUCCESS) {
			return checkAccountLogin;
		}

		return signupService.deleteSignup(signupId);

	}

}
